import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:xvideos/hj_model/home/home_tabs_entity.dart';
import 'package:xvideos/hj_model/home/module_detail_model.dart';
import 'package:xvideos/hj_page/community_business/widget/community_post_widget.dart';
import 'package:xvideos/hj_page/community_business/widget/game_item.dart';
import 'package:xvideos/hj_page/community_business/widget/pic_simple_cell_widget.dart';
import 'package:xvideos/hj_utils/api_service/vid_service.dart';

enum PageStyle {
  list,
  grid,
  waterFlow;
}

abstract class BaseSourceModuleLogic extends GetxController {
  final ModuleData model;
  BaseSourceModuleLogic(this.model);
  final dataSource = [];
  final showSort = false;
  final PageStyle pageStyle = PageStyle.grid;
  final double ratio = 1;
  final int crossAxisCount = 3;
  final double spaceV = 14.h;
  final double spaceH = 10.w;
  final bool showSection = false;
  RefreshController? refreshController;
  int page = 1;
  bool isLoading = true;
  int sort = 1;
  final sections = <AllSection>[];

  void onReady() {
    super.onReady();
    fetchPagedata();
  }

  Future<ModuleDetailModel?> fetchPagedata({bool isRefresh = true, bool showLoading = false}) async {
    if (isRefresh) {
      page = 1;
    }
    if (showLoading) {
      isLoading = true;
      update();
    }
    final res = await VidService.getModuleDetail(model.id ?? '', pageNumber: page, pageSize: 10, moduleSort: sort);
    isLoading = false;
    return res;
  }

  Widget instanceChild(int index);
}

class GameSourceController extends BaseSourceModuleLogic {
  GameSourceController(super.model);

  @override
  PageStyle get pageStyle => PageStyle.grid;
  @override
  int get crossAxisCount => 2;
  @override
  double get ratio => 191 / 154;

  @override
  Widget instanceChild(int index) {
    return GameItem(dataSource[index]);
  }

  @override
  Future<ModuleDetailModel?> fetchPagedata({bool isRefresh = true, bool showLoading = false}) async {
    final res = await super.fetchPagedata(isRefresh: isRefresh, showLoading: showLoading);
    if (isRefresh) {
      refreshController?.refreshCompleted();
      dataSource.clear();
    }
    if (res?.hasNext ?? false) {
      refreshController?.loadComplete();
    } else {
      refreshController?.loadNoData();
    }
    dataSource.addAll(res?.allVideoInfo ?? []);
    page += 1;
    update();
    return null;
  }
}

class PictureSourceController extends BaseSourceModuleLogic {
  PictureSourceController(super.model);

  @override
  PageStyle get pageStyle => PageStyle.waterFlow;
  final ratios = [191 / 132, 191 / 278, 191 / 278, 191 / 132];

  @override
  Future<ModuleDetailModel?> fetchPagedata({bool isRefresh = true, bool showLoading = false}) async {
    final res = await super.fetchPagedata(isRefresh: isRefresh, showLoading: showLoading);
    if (isRefresh) {
      refreshController?.refreshCompleted();
      dataSource.clear();
    }
    if (res?.hasNext ?? false) {
      refreshController?.loadComplete();
    } else {
      refreshController?.loadNoData();
    }
    dataSource.addAll(res?.allVideoInfo ?? []);
    page += 1;
    update();
    return null;
  }

  @override
  Widget instanceChild(int index) {
    return AspectRatio(
      aspectRatio: ratios[index % 4],
      child: PicSimpleCellWidget(
        videoModel: dataSource[index],
      ),
    );
  }
}

class SeedSourceController extends BaseSourceModuleLogic {
  SeedSourceController(super.model);
  @override
  PageStyle get pageStyle => PageStyle.list;

  @override
  Future<ModuleDetailModel?> fetchPagedata({bool isRefresh = true, bool showLoading = false}) async {
    final res = await super.fetchPagedata(isRefresh: isRefresh, showLoading: showLoading);
    if (isRefresh) {
      refreshController?.refreshCompleted();
      dataSource.clear();
    }
    if (res?.hasNext ?? false) {
      refreshController?.loadComplete();
    } else {
      refreshController?.loadNoData();
    }
    dataSource.addAll(res?.allVideoInfo ?? []);
    page += 1;
    update();
    return null;
  }

  @override
  Widget instanceChild(int index) {
    return CommunityPostWidget(videoModel: dataSource[index]);
  }
}

BaseSourceModuleLogic instanceController(ModuleData module) {
  switch (module.showType) {
    case 1:
      return GameSourceController(module);
    case 3:
      return SeedSourceController(module);
    case 4:
      return PictureSourceController(module);

    default:
      throw '${module.showType} 没有定义这种类型';
  }
}
